package com.bgl.concurrency.commonUnsafe;

import com.bgl.concurrency.annotations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * Created by BGL on 2018/3/24.
 */
@Slf4j
@ThreadSafe
public class DateFormatTest2 {

    private static int clientTotal = 10000;

    private static int threadTotal = 200;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Semaphore semaphore = new Semaphore(threadTotal);
        CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        for (int i = 0; i < clientTotal; i++) {
            executorService.execute(() -> {
                try{
                    semaphore.acquire();
                    update();
                    semaphore.release();
                } catch (Exception e) {
                    log.info("exception : ", e);
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();

//        log.info("count : {}", count.length());
    }

    private static void update() {
        try{
            /**
             * 线程安全，由于局部变量线程关闭
             */
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            format.parse("20180324");
        } catch (Exception e) {
            log.info("parse exception : ", e);
        }
    }
}
